package com.gome.service;


import com.gome.entity.Menu;
import com.gome.entity.MenuLevel;
import com.gome.mapper.MenuMapper;
import com.gome.mapper.RoleMenuMapper;
import com.gome.utils.DateUtils;
import com.gome.utils.Result;
import com.gome.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MenuService  {
    @Autowired
    public MenuMapper MenuMapper;
    @Autowired
    public RoleMenuMapper RoleMenuMapper;
    @Autowired
    public BMService BMService;
    public Result queryAllMenu(int level) {

        List<Menu> menuList = MenuMapper.selecMenuListWithNext(level);
        if(null!=menuList&&menuList.size()>0) {
            return new Result(ResultCode.SUCCESS, "查询菜单成功",  menuList);
        }else{
            return new Result(ResultCode.FAIL, "查询菜单异常", null);
        }
    }
    public Result getAllMenuLevel() {

        List<MenuLevel> menuLevelList = MenuMapper.selectMenuLevelList();
        if(null!=menuLevelList&&menuLevelList.size()>0) {
            return new Result(ResultCode.SUCCESS, "查询菜单等级成功",  menuLevelList);
        }else{
            return new Result(ResultCode.FAIL, "查询菜单等级异常", null);
        }
    }

    public Result addMenu(Menu menu) {
        Menu mlin= MenuMapper.selectMenuByVcMenumc(menu);
            if(null!=mlin)  return new Result(ResultCode.FAIL, "菜单名称["+menu.getVcMenumc()+"]已存在",  null);
        String ocMenubm = BMService.createBM("MENU" + DateUtils.formatDate(new Date(), DateUtils.YYMM), "00000", "sys_menu", menu.getOcCzy());
        menu.setDtCzrq(new Date());
        menu.setOcMenubm(ocMenubm);
        if(null!=menu.getOcParentbm()&&!"".equals(menu.getOcParentbm())){
            Menu  m= MenuMapper.selectMenulevelByOcMenubm(menu.getOcParentbm());
            menu.setnLevel(m.getnLevel());
            menu.setnOrder(m.getnOrder());
        }else{
            menu.setnLevel(1);
            menu.setnOrder(1);
        }
        MenuMapper.addMenu(menu);
        //维护角色-菜单权限 默认与父类菜单相同的权限
        if(null!=menu.getOcParentbm()&&!"".equals(menu.getOcParentbm()))  RoleMenuMapper.addRoleMenuByMenu(menu);

       return new Result(ResultCode.SUCCESS, "添加菜单成功",  null);
    }

    public Result updateMenu(Menu menu) {
        Menu m= MenuMapper.selectMenuByVcMenumc(menu);
        if(null!=m)  return new Result(ResultCode.FAIL, "菜单名称["+menu.getVcMenumc()+"]已存在",  null);
        MenuMapper.updateMenu(menu);
        return new Result(ResultCode.SUCCESS, "修改菜单成功",  menu);
    }

    public Result deleteMenu(Menu menu)  {

     //   MenuMapper.deleteMenu(menu);
        List<String> list= new ArrayList<String>();


        list.add(menu.getOcMenubm());
        List<String> next = getNext(menu.getOcMenubm(),list);
        //固定报表需要处理？？？
        //删除角色菜单权限
        for(String OcMenubm:next){
            RoleMenuMapper.deleteRoleMenu(OcMenubm);
        }

        //删除菜单
        for(String OcMenubm:next){
            MenuMapper.deleteMenu(OcMenubm);
        }

        return new Result(ResultCode.SUCCESS, "删除菜单成功",  menu);
    }


    private List<String> getNext(String ocParentbm,List<String> list) {
        List<Menu> Menus = MenuMapper.queryNextMenu(ocParentbm);
        for(Menu menu:Menus){
            list.add(menu.getOcMenubm());
            getNext(menu.getOcMenubm(), list);

        }
        return list;

    }

    //不查下级
    public Result selectMenuList(int nLevel) {

        List<Menu> menuList = MenuMapper.selectMenuList(nLevel);
        if(null!=menuList&&menuList.size()>0) {
            return new Result(ResultCode.SUCCESS, "查询菜单成功",  menuList);
        }else{
            return new Result(ResultCode.FAIL, "查询菜单异常", null);
        }
    }

}
